<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="基地名称">
              <el-input v-model="query.baseName" placeholder="输入基地名称" clearable> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="基地状态 ">
              <el-select v-model="query.enabledMark" placeholder="请选择基地状态 " clearable>
                <el-option v-for="(item, index) in enabledmarkOptions" :key="index" :label="item.fullName"
                  :value="item.id" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间">
              <el-date-picker v-model="query.creatorTime" value-format="timestamp" format="yyyy-MM-dd"
                start-placeholder="开始日期" end-placeholder="结束日期" type="daterange" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <el-button v-has="'btn_add'" type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增
            </el-button>
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false" @click="initData()" />
            </el-tooltip>
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list" @sort-change='sortChange' :span-method="arraySpanMethod">
          <el-table-column prop="baseName" label="基地名称" align="left" show-overflow-tooltip />
          <el-table-column prop="landArea" label="用地面积" align="left" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.landArea >= 0 && !scope.row.landArea == '') ? scope.row.landArea+'㎡':''}}
            </template>
          </el-table-column>
          <el-table-column prop="buildArea" label="建筑面积" align="left" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.buildArea >= 0 && !scope.row.buildArea == '') ? scope.row.buildArea+'㎡':''}}
            </template>
          </el-table-column>
          <el-table-column prop="leadIndustry" label="主导产业" align="left" show-overflow-tooltip />
          <el-table-column prop="settledEnNum" label="入驻企业数" align="left" min-width="80" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.settledEnNum >= 0 && !scope.row.settledEnNum == '') ? scope.row.settledEnNum+'家':''}}
            </template>
          </el-table-column>
          <el-table-column prop="techEnNum" label="科技型企业数" align="left" width="110" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.techEnNum >= 0 && !scope.row.techEnNum == '') ? scope.row.techEnNum+'家':''}}
            </template>
          </el-table-column>
          <el-table-column prop="specNewEnNum" label="专精特新企业数" align="left" min-width="110" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.specNewEnNum >= 0 && !scope.row.specNewEnNum == '') ? scope.row.specNewEnNum+'家':''}}
            </template>
          </el-table-column>
          <el-table-column prop="standardSizeEnNum" label="规上企业数" align="left" width="110" show-overflow-tooltip >
            <template slot-scope="scope">
              {{(scope.row.standardSizeEnNum >= 0 && !scope.row.standardSizeEnNum == '') ? scope.row.standardSizeEnNum+'家':''}}
            </template>
          </el-table-column>
          <el-table-column label="基地状态 " prop="enabledMark" algin="left">
            <template slot-scope="scope">
              <el-tag :type="scope.row.enabledMark == 1 ? 'success' : 'danger'" disable-transitions>
                {{scope.row.enabledMark==1?'启用':'禁用'}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="description" label="备注" align="left" show-overflow-tooltip />
          <el-table-column prop="creatorUserName" label="创建人" align="left" width="100" show-overflow-tooltip />
          <el-table-column prop="creatorTime" label="创建时间" align="left" width="150" />
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button v-has="'btn_edit'" type="text" @click="addOrUpdateHandle(scope.row.id)">编辑
              </el-button>
              <el-button v-has="'btn_remove'" type="text" class="JNPF-table-delBtn" @click="handleDel(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
          @pagination="initData" />
      </div>
    </div>
    <JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" />
  </div>
</template>
<script>
  import request from '@/utils/request'
  import JNPFForm from './Form'
  import { getDataInterfaceRes, getDictionaryDataSelector, getList, del,  } from '@/api/asso/base/base'
  export default {
    components: {
      JNPFForm
    },
    data() {
      return {
        expandObj: {},
        columnOptions: [],
        mergeList: [],
        exportList: [],
        query: {
          baseName: '',
          enabledMark: '',
          creatorTime:null,
        },
        treeProps: {
          children: 'children',
          label: 'fullName',
          value: 'id'
        },
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          currentPage: 1,
          pageSize: 20,
          sort: "desc",
          sidx: "",
        },
        formVisible: false,
        exportBoxVisible: false,
        enabledmarkOptions: [{
          "fullName": "禁用",
          "id": "0"
        }, {
          "fullName": "启用",
          "id": "1"
        }],
        enabledmarkProps: {
          "label": "fullName",
          "value": "id"
        },
      }
    },
    computed: {
      menuId() {
        return this.$route.meta.modelId || ''
      }
    },
    created() {
        this.initData()
    },
    methods: {
      arraySpanMethod({
        column
      }) {
        for (let i = 0; i < this.mergeList.length; i++) {
          if (column.property == this.mergeList[i].prop) {
            return [this.mergeList[i].rowspan, this.mergeList[i].colspan]
          }
        }
      },
      getMergeList(list) {
        let newList = JSON.parse(JSON.stringify(list))
        newList.forEach(item => {
          if (item.children && item.children.length) {
            let child = {
              prop: item.prop + '-child-first'
            }
            item.children.unshift(child)
          }
        })
        newList.forEach(item => {
          if (item.children && item.children.length) {
            item.children.forEach((child, index) => {
              if (index == 0) {
                this.mergeList.push({
                  prop: child.prop,
                  rowspan: 1,
                  colspan: item.children.length
                })
              } else {
                this.mergeList.push({
                  prop: child.prop,
                  rowspan: 0,
                  colspan: 0
                })
              }
            })
          } else {
            this.mergeList.push({
              prop: item.prop,
              rowspan: 1,
              colspan: 1
            })
          }
        })
      },
      sortChange({
        column,
        prop,
        order
      }) {
        this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
        this.listQuery.sidx = !order ? '' : prop
        this.initData()
      },
      initData() {
        this.listLoading = true;
        let _query = {
          ...this.listQuery,
          ...this.query,
          keyword: this.keyword,
          dataType: 0,
          menuId: this.menuId
        }
        getList(_query).then(res => {
          var _list = [];
          for (let i = 0; i < res.data.list.length; i++) {
            let _data = res.data.list[i];
            _list.push(_data)
          }
          this.list = _list.map(o => ({
            ...o,
            ...this.expandObj
          }))
          this.total = res.data.pagination.total
          this.listLoading = false
        })
      },
      handleDel(id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          type: 'warning'
        }).then(() => {
          del(id).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            });
          })
        }).catch(() => {});
      },
      addOrUpdateHandle(id) {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs.JNPFForm.init(id)
        })
      },
      search() {
        this.listQuery.currentPage = 1
        this.listQuery.pageSize = 20
        this.listQuery.sort = "desc"
        this.listQuery.sidx = ""
        this.initData()
      },
      refresh(isrRefresh) {
        this.flowVisible = false
        if (isrRefresh) this.reset()
      },
      reset() {
        for (let key in this.query) {
          this.query[key] = undefined
        }
        this.search()
      }
    }
  }
</script>
<style>
.el-tooltip__popper{
  max-width:20%
}
</style>
